package com.yipei.business.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yipei.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yipei.business.mapper.MlTodayNewsMapper;
import com.yipei.business.domain.MlTodayNews;
import com.yipei.business.service.IMlTodayNewsService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
 * 每日解读Service业务层处理
 * 
 * @author yipei
 * @date 2024-12-23
 */
@Service
public class MlTodayNewsServiceImpl implements IMlTodayNewsService 
{
    @Autowired
    private MlTodayNewsMapper mlTodayNewsMapper;

    /**
     * 查询每日解读
     * 
     * @param id 每日解读主键
     * @return 每日解读
     */
    @Override
    public MlTodayNews selectMlTodayNewsById(Long id)
    {
        return mlTodayNewsMapper.selectMlTodayNewsById(id);
    }

    /**
     * 查询每日解读列表
     * 
     * @param mlTodayNews 每日解读
     * @return 每日解读
     */
    @Override
    public List<MlTodayNews> selectMlTodayNewsList(MlTodayNews mlTodayNews)
    {
        return mlTodayNewsMapper.selectMlTodayNewsList(mlTodayNews);
    }

    /**
     * 新增每日解读
     * 
     * @param mlTodayNews 每日解读
     * @return 结果
     */
    @Override
    public int insertMlTodayNews(MlTodayNews mlTodayNews)
    {
        mlTodayNews.setCreateTime(DateUtils.getNowDate());
        return mlTodayNewsMapper.insertMlTodayNews(mlTodayNews);
    }

    /**
     * 修改每日解读
     * 
     * @param mlTodayNews 每日解读
     * @return 结果
     */
    @Override
    public int updateMlTodayNews(MlTodayNews mlTodayNews)
    {
        mlTodayNews.setUpdateTime(DateUtils.getNowDate());
        return mlTodayNewsMapper.updateMlTodayNews(mlTodayNews);
    }

    /**
     * 批量删除每日解读
     * 
     * @param ids 需要删除的每日解读主键
     * @return 结果
     */
    @Override
    public int deleteMlTodayNewsByIds(Long[] ids)
    {
        return mlTodayNewsMapper.deleteMlTodayNewsByIds(ids);
    }

    /**
     * 删除每日解读信息
     * 
     * @param id 每日解读主键
     * @return 结果
     */
    @Override
    public int deleteMlTodayNewsById(Long id)
    {
        return mlTodayNewsMapper.deleteMlTodayNewsById(id);
    }

    @Override
    public void addTodayNews() throws IOException {
        HttpClient client = HttpClients.createDefault();
        HttpGet request = new HttpGet("https://np-listapi.eastmoney.com/comm/web/getNewsByColumns?client=web&biz=web_news_col&column=344&order=1&needInteractData=0&page_index=1&page_size=20&req_trace=1735628072621"); //示例URL，请替换为实际的股票代码
        request.setHeader("Content-Type", "text/javascript;charset=UTF-8");
        HttpResponse response = client.execute(request);
        List<MlTodayNews> list= new ArrayList<>();
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity,"UTF-8");
            JSONObject jsonObject =  JSONObject.parseObject(result).getJSONObject("data");
            JSONArray list1 = jsonObject.getJSONArray("list");
            list = list1.toJavaList(MlTodayNews.class).subList(0,20);
            for (MlTodayNews mlTodayNews : list) {
                mlTodayNews.setContent(mlTodayNews.getSummary());
                mlTodayNews.setMrSj(mlTodayNews.getShowTime());
                mlTodayNews.setMrLx(mlTodayNews.getMediaName());
            }
        }
        for (MlTodayNews mlTodayNews : list) {
            this.insertMlTodayNews(mlTodayNews);
        }
    }
}
